/**
        Copyright (c) 2009 ATTi 
        All rights reserved.
*/

@isTest
private class caseRoundRobinAccountUpdateTester {
	private static User admin, u_new;
	
	private static void addUsers(){
	    admin = new User();
		admin.UserName = 'xyz_1234567890@atti-temp.com' + 'x';
		admin.LastName = 'Michael Shu Test Email' + 'x';
		admin.Email = 'xyz_1234567890@atti.com';
		admin.Alias = 'abcde' + 'x';
		admin.CommunityNickname = 'xyaz_1234556' + 'x';
		admin.TimeZoneSidKey = 'America/Los_Angeles';
		admin.LocaleSidKey = 'en_US';
		admin.EmailEncodingKey = 'UTF-8';
		admin.ProfileId = [select ID from Profile where Name = 'System Administrator' limit 1].Id;
		admin.UserRoleId = [select ID from UserRole where Name = 'Information Systems' limit 1].Id;
		admin.LanguageLocaleKey = 'en_US';
		insert admin;
		
		u_new = new User();
		u_new.UserName = 'IOG_test' + '@test.com' ;
		u_new.LastName = 'Michael Shu Test Email';
		u_new.Email = 'xyz_1234567890@atti.com';
		u_new.Alias = 'abcde';
		u_new.CommunityNickname = 'xyaz_1234556';
		u_new.TimeZoneSidKey = 'America/Los_Angeles';
		u_new.LocaleSidKey = 'en_US';
		u_new.EmailEncodingKey = 'UTF-8';
		u_new.Assignment_Group_Active__c = true;
		u_new.IsActive = true;
		u_new.ProfileId = [select ID from Profile where Name like 'IOG%' limit 1].Id;
		u_new.LanguageLocaleKey = 'en_US';
		u_new.UserRoleId = [select ID from UserRole where Name like 'IOG%' limit 1].Id;
		insert u_new;   			
	}
	
	private static ID createAssignmentGroup(){
    	Assignment_Group_Name__c new_assignment_group = new Assignment_Group_Name__c();
    	new_assignment_group.Name = 'new test group';
    	new_assignment_group.Type__c = 'Cases';
    	System.runAs(admin){
    		insert new_assignment_group; 
    	}
    	
    	Group g = new Group();
    	g.Name = 'new test group';
    	g.Type = 'Queue';
    	System.runAs(admin){
    		insert g;
    	}

    	QueueSobject qsobject = new QueueSobject();
    	qsobject.QueueId = g.Id;
    	qsobject.SobjectType = 'Case';
     	System.runAs(admin){
    		insert qsobject;
    	}   	
    	
    	Assignment_Group_Queues__c new_AG_Queue = new Assignment_Group_Queues__c();
    	Group q = [select Id, Name from Group where id =: g.Id limit 1];
    	System.debug(' q.Name = ' + q.Name + '; q.Id = ' + q.Id);
    	new_AG_Queue.Name = q.Name;
    	new_AG_Queue.Assignment_Group_Name__c = new_assignment_group.Id;
    	new_AG_Queue.Active__c = 'true';
    	new_AG_Queue.QueueId__c = q.Id;
    	System.runAs(admin){
    		insert new_AG_Queue;
    	}
    	Assignment_Group_Queues__c ag_queue = [select id, QueueId__c, Assignment_Group_Name__c, Valid_Queue__c, Active__c, Assignment_Group_Name__r.Type__c from Assignment_Group_Queues__c where id =: new_AG_Queue.Id limit 1];
    	System.assertEquals(true, ag_queue.Valid_Queue__c);
    	System.assertEquals('True', ag_queue.Active__c);
    	System.assertEquals('Cases', ag_queue.Assignment_Group_Name__r.Type__c);
    	System.assertEquals(q.Id, ag_queue.QueueId__c);
    	System.assertEquals(new_assignment_group.Id, ag_queue.Assignment_Group_Name__c);
    	
    	Assignment_Groups__c new_group_members = new Assignment_Groups__c();
    	new_group_members.Active__c = 'true';
    	new_group_members.Group_Name__c = new_assignment_group.Id;
    	new_group_members.User__c = u_new.Id;
    	new_group_members.Last_Assignment__c = Datetime.now();
    	new_group_members.Millisecond__c = 0;
    	System.runAs(admin){
    		insert new_group_members;
    	}
    	Assignment_Groups__c newmember = [select id,User_Active__c,Group_Name__r.Type__c,Group_Name__c,Active__c, Status__c, Last_Assignment__c, Millisecond__c from Assignment_Groups__c where id =: new_group_members.Id];
    	System.assertEquals('<img src="/img/samples/light_green.gif" alt="status color" border="0"/>', newmember.Status__c);
    	System.assertEquals('True',newmember.User_Active__c);
    	System.assertEquals('Cases',newmember.Group_Name__r.Type__c);
    	System.assertEquals(new_assignment_group.Id,newmember.Group_Name__c);
    	System.assertEquals('True',newmember.Active__c);
    	System.assertNotEquals(null,newmember.Last_Assignment__c);
    	System.assertEquals(0,newmember.Millisecond__c);
    	return g.Id;
	}
   //created by Michael Shu
    static testMethod void testAccountUpdate() {
    	Case cs = new Case();
    	Contact ct = new Contact(LastName = 'TestContact_1111', AccountId = null);
    	Account acc = new Account(Name = 'TestAccount_1111');
    	insert ct;
    	insert acc;
    	Contact[] ct_rst = [select Id, AccountId, Name from Contact where LastName =: 'TestContact_1111'];
    	Account[] acc_rst = [select Id from Account where Name =: 'TestAccount_1111'];
    	
		cs.RecordTypeId = RecordTypeHelper.GetRecordTypeID('Case', 'IOG Incident');
		cs.CSS_CRF_RequestType__c = 'Inquiry';
		cs.CSS_ProductFamily__c = 'Product1;Product2';
		cs.Status = 'Open';
		cs.Origin = 'Phone';
		cs.Priority = 'High';
		cs.Subject = 'TestSub_1111';
		Database.SaveResult case_rst = Database.insert(cs);  
	    Case[] cs_rst = [select Id, AccountId, ContactId from Case where id =: case_rst.getId()];
		cs.AccountId = acc_rst[0].Id;
		cs.ContactId = ct_rst[0].Id;	    
	    update cs;
	    	
	    Contact[] ct_rst_after = [select Id, AccountId, Name from Contact where LastName =: 'TestContact_1111'];
	    
	    System.assertEquals(acc_rst[0].Id, ct_rst_after[0].AccountId);
    }
    	
     //created by Michael Shu	
    static testMethod void testRoundRobin() {
    	addUsers();
    	Case cs = new Case();
    	cs.Subject = 'TestRR_1111';
    	cs.Origin = 'Phone';
    	cs.Status = 'Open';
    	cs.IR_Priority__c = 'Standard';
    	cs.IOG_Medal__c = 'Bronze';
		cs.RecordTypeId = RecordTypeHelper.GetRecordTypeID('Case', 'IOG Incident');
		cs.CSS_CRF_RequestType__c = 'Not Defined';
		cs.CSS_ProductFamily__c = 'Product1;Product2';
		System.runAs(admin){
			insert cs;
		}
		Case cs_rst = [select id, ownerId from Case where id =: cs.id];
		System.assertEquals(admin.id, cs_rst.OwnerId);
    	
    	List<Assignment_Group_Name__c> assignmentGroups = [select id, (select User__c, User__r.Name from Assignment_Groups__r where Active__c =: 'true' and User_Active__c =: 'true' and Group_Name__r.Type__c = 'Cases')
    	                                                            , (select QueueId__c, Name from Assignment_Group_Queues__r where Valid_Queue__c  =: true and Active__c =: 'true' and Assignment_Group_Name__r.Type__c = 'Cases') 
    	                                                   from Assignment_Group_Name__c] ;
    	Set<ID> memeberIDs = new Set<ID>();
    	ID QueueId;
    	if(!assignmentGroups.isEmpty()){
    		for(Assignment_Group_Name__c ag : assignmentGroups){
    			if(ag.Assignment_Groups__r != null && !ag.Assignment_Groups__r.isEmpty() && ag.Assignment_Group_Queues__r != null && !ag.Assignment_Group_Queues__r.isEmpty()){
    				QueueId = ag.Assignment_Group_Queues__r[0].QueueId__c;
    				System.debug('add Queue Name ' + ag.Assignment_Group_Queues__r[0].Name + ' Queue ID = ' + QueueId);
    				for(Assignment_Groups__c p : ag.Assignment_Groups__r){
    					memeberIDs.add(p.User__c);
    					System.debug('add p.User__c name ' + p.User__r.Name);
    				}
    				break;
    			}
    		}
    	}
    	if(QueueId != null){
	    	cs_rst.OwnerId = QueueId;
	    	System.debug(' QueueId = ' + QueueId);
	    	
	    	System.runAs(admin){
	    		update cs_rst; 
	    	}	
	    	Case cs_rst_after = [select Id, OwnerId, Owner.Name from Case where Id =: cs.Id];
	    	System.debug('cs_rst_after.Owner.Name = ' + cs_rst_after.Owner.Name);
	    	if(!memeberIDs.isEmpty()){
	    		System.assertEquals(true, memeberIDs.contains(cs_rst_after.OwnerId));
	    	}
    	}

    }
}